<script setup lang="ts">
import { reactive, ref } from 'vue'
import { User, Lock } from '@element-plus/icons-vue'
import type { FormInstance, FormRules } from 'element-plus'
import { useRouter } from 'vue-router'

const router = useRouter()

// 登录表单数据
const loginForm = reactive({
  username: '',
  password: '',
})

// 表单验证规则
const rules = reactive<FormRules>({
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '用户名长度应为3-20个字符', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '密码长度应为6-20个字符', trigger: 'blur' },
  ],
})

// 表单引用
const loginFormRef = ref<FormInstance>()
const loading = ref(false)

// 登录方法
const handleLogin = async (formEl: FormInstance | undefined) => {
  if (!formEl) return

  await formEl.validate((valid, fields) => {
    if (valid) {
      loading.value = true
      // 模拟登录请求
      setTimeout(() => {
        console.log('登录成功', loginForm)
        loading.value = false
        // 登录成功后跳转到首页
        router.push('/')
      }, 2000)
    } else {
      console.log('表单验证失败', fields)
    }
  })
}

// 重置表单
const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
}
</script>

<template>
  <div class="login_container">
    <!-- 左侧背景区域 -->
    <div class="login_left">
      <div class="login_left_content">
        <h1 class="system_title">硅谷甄选运营平台</h1>
        <p class="system_desc">一个现代化的后台管理系统</p>
      </div>
    </div>

    <!-- 右侧登录表单 -->
    <div class="login_right">
      <div class="login_form">
        <div class="login_form_content">
          <h2 class="login_title">用户登录</h2>

          <el-form
            ref="loginFormRef"
            :model="loginForm"
            :rules="rules"
            size="large"
            status-icon
          >
            <el-form-item prop="username">
              <el-input
                v-model="loginForm.username"
                placeholder="请输入用户名"
                :prefix-icon="User"
                clearable
              />
            </el-form-item>

            <el-form-item prop="password">
              <el-input
                v-model="loginForm.password"
                type="password"
                placeholder="请输入密码"
                :prefix-icon="Lock"
                show-password
                clearable
              />
            </el-form-item>

            <el-form-item>
              <el-button
                type="primary"
                size="large"
                :loading="loading"
                @click="handleLogin(loginFormRef)"
                style="width: 100%"
              >
                {{ loading ? '登录中...' : '登录' }}
              </el-button>
            </el-form-item>

            <el-form-item>
              <el-button
                size="large"
                @click="resetForm(loginFormRef)"
                style="width: 100%"
              >
                重置
              </el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped lang="scss">
.login_container {
  width: 100%;
  height: 100vh;
  display: flex;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

/* 左侧背景区域 */
.login_left {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(
    135deg,
    rgba(102, 126, 234, 0.8) 0%,
    rgba(118, 75, 162, 0.8) 100%
  );
  position: relative;
  overflow: hidden;
}

.login_left::before {
  content: '';
  position: absolute;
  top: -50%;
  left: -50%;
  width: 200%;
  height: 200%;
  background: url('../../assets/images/background.jpg') no-repeat center;
  background-size: cover;
  opacity: 0.3;
  z-index: 1;
}

.login_left_content {
  text-align: center;
  color: white;
  z-index: 2;
  position: relative;
}

.system_title {
  font-size: 48px;
  font-weight: 700;
  margin-bottom: 20px;
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3);
}

.system_desc {
  font-size: 18px;
  opacity: 0.9;
  margin: 0;
}

/* 右侧登录表单区域 */
.login_right {
  width: 500px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  padding: 40px;
}

.login_form {
  width: 100%;
  max-width: 400px;
}

.login_form_content {
  padding: 0;
}

.login_title {
  text-align: center;
  margin-bottom: 40px;
  color: #333;
  font-size: 28px;
  font-weight: 600;
}

/* 表单项样式 */
:deep(.el-form-item) {
  margin-bottom: 24px;
}

:deep(.el-input__wrapper) {
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
  height: 50px;
}

:deep(.el-input__wrapper:hover) {
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

:deep(.el-input__wrapper.is-focus) {
  box-shadow: 0 4px 12px rgba(64, 158, 255, 0.3);
}

:deep(.el-input__inner) {
  height: 48px;
  line-height: 48px;
  font-size: 16px;
}

:deep(.el-button) {
  border-radius: 8px;
  font-weight: 500;
  transition: all 0.3s ease;
  height: 50px;
  font-size: 16px;
}

:deep(.el-button--primary) {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border: none;
}

:deep(.el-button--primary:hover) {
  background: linear-gradient(135deg, #5a6fd8 0%, #6a4190 100%);
  transform: translateY(-2px);
  box-shadow: 0 8px 20px rgba(102, 126, 234, 0.4);
}

:deep(.el-button--default) {
  background: #f8f9fa;
  border: 1px solid #e9ecef;
  color: #6c757d;
}

:deep(.el-button--default:hover) {
  background: #e9ecef;
  border-color: #dee2e6;
  color: #495057;
}

/* 响应式设计 */
@media (max-width: 1024px) {
  .login_left {
    display: none;
  }

  .login_right {
    width: 100%;
  }
}

@media (max-width: 768px) {
  .login_right {
    padding: 20px;
  }

  .login_form {
    max-width: 100%;
  }

  .system_title {
    font-size: 36px;
  }

  .login_title {
    font-size: 24px;
    margin-bottom: 30px;
  }
}
</style>
